#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<errno.h>
void* my_memset(void* dest, int c, size_t count)
{
	assert(dest);
	void* ret = dest;
	while (count--)
	{
		*((char*)dest + count) = c;
	}
	return ret;
}
int my_memcmp(const void* s1, const void* s2,size_t cnt)
{
	assert(s1 && s2);

	while (*(char*)s1 == *(char*)s2)
	{
		if (cnt == 0)
			return 0;
		s1= (char*)s1+1;
		s2= (char*)s2+1;
		cnt--;
	}
	int ret = *(char*)s1 - *(char*)s2;
	return ((-ret < 0)) - (ret < 0);
}
void* my_memmove(void* dest, const void* src, size_t cnt)
{
	assert(dest && src);
	void* ret = dest;
	if (dest < src)
	{
		while (cnt--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		while (cnt--)
		{
			*( (char*)dest + cnt) = *((char*)src + cnt);
		}
	}
	return ret;
}
void* my_memcpy(void* dest, const void* src, size_t cnt)
{
	assert(dest && src);
	void* ret = dest;
	while (cnt--)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}
static char* ret = NULL;
char* my_strtok(char* str1, const char* str2)
{
	char* s1, * s2,*str = NULL;
	if (str1 == NULL)//如果为空，就从ret这个静态全局变量取上一个的地址
	{
		 s1 = ret;
		 s2 = (char *)str2;
		 str = ret;
	}
	else//否则，正常进行初始化
	{
		 s1 = str1;
		 s2 = (char*)str2;
		 ret = str1;
		 str = ret;
	}
	if (s1 == NULL)//如果s1 == NULL说明 ret 为空，则返回空值，说明这轮调用结束了
		return NULL;
	while (*s1)
	{
		s2 = (char*)str2;//每回循环结束让s2回到原本的位置，s1++后再逐个比较
		while (*s1 != *s2 && *s2!='\0'&& *s1!='\0')
		{
			s2++;
		}
		if (*s1 == *s2 && *s1 != '\0')
		{
			*s1 = '\0';
			ret = ++s1;
			return str;
		}
		s1++;
	}
	ret = NULL;//上个循环的结束条件是s1 ==0，也就是说已经走到尽头了，避免无限循环，把ret赋值NULL，下次调用就会直接结束
	return str;
}
char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	const char* dec = str1;
	const char* str = str2;

	const char* cur = str1;
	while (*cur)
	{
		dec = cur;
		str = str2;
		while (*dec && *str && (*dec == *str))
		{
			dec++;
			str++;
		}
		if (*str == '\0')
			return (char *)cur;

		cur++;
	}
	return NULL;
}
int my_strcmp(const char* s1, const char* s2)
{
	assert(s1 && s2);

	while (*s1 == *s2)
	{
		if (*s1 == '\0')
			return 0;
		s1++;
		s2++;
	}
	int ret = *s1 - *s2;
	return ((-ret < 0)) - (ret < 0);
}
char* my_strcat(char* det, const char* src)
{
	assert(det && src);
	char* s1 = det;
	while (*det)
	{
		det++;
	}
	while (*det++ = *src++)
	{
		;
	}
	return s1;
}
char* my_strcpy(char* det, const char* src)
{
	assert(det && src);
	char* s1 = det;
	while (*det++ = *src++)
	{
		;
	}
	return s1;
}
size_t my_strlen(const char *str)
{
	int cnt = 0;
	while (*str != '\0')
	{
		cnt++;
		str++;
	}
	return cnt;
}
int main()
{
	int a[5] = { 0,1,2,3,4};
	my_memset(a, 5, 20);
	return 0;
}

